﻿namespace WebPacker.Tool.Tests.Sources
{
	using System.Text;
	using NUnit.Framework;
	using WebPacker.Configuration;
	using WebPacker.Packs;
	using WebPacker.Tool.Sources;
	using WebPacker.Tool.Tests.EqualityComparers;

	[TestFixture]
	public class PacksSourceTest
	{
		[Test]
		public void OpenPacksTest()
		{
			var source = new PacksSource(Configuration.GetDefaults(), "samples/packs2.xml");
			source.Open();

			var expected = new PackInfo[]
			{
				new PackInfo
				{
					Name = "scripts",
					PackType = PackType.JavaScript,
					BasePath = @"~\samples", // overrided path
					FilterName = "js",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = "test1.js", Encoding = Encoding.GetEncoding(1251) },
						new ResourceInfo { Name = "test2.js" }
					}
				},
				new PackInfo
				{
					Name = "styles",
					PackType = PackType.Css,
					FilterName = "css",
					VersionGeneratorName = "manual",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = @"samples\style1.css" }
					}
				},
			};

			var configuration = Configuration.GetDefaults();
			configuration.VersionGeneratorName = "md5";
			ConfigurationTest.AssertConfiguraionEquals(configuration, source.Configuration);

			var actual = source.Packs;
			Assert.IsNotNull(actual);
			Assert.That(actual, Is.EquivalentTo(expected).Using(new PackInfoEqualityComparer()));
		}

		[Test]
		public void OpenSimplePacksTest()
		{
			var source = new PacksSource(Configuration.GetDefaults(), "samples/packs1.xml");
			source.Open();

			var expected = new PackInfo[]
			{
				new PackInfo
				{
					PackType = PackType.JavaScript,
					Name = "scripts",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = "test1.js" },
						new ResourceInfo { Name = "test2.js" }
					}
				},
				new PackInfo
				{
					PackType = PackType.Css,
					Name = "styles",
					Resources = new ResourceInfo[]
					{
						new ResourceInfo { Name = "style1.css" }
					}
				}
			};

			ConfigurationTest.AssertConfiguraionEquals(Configuration.GetDefaults(), source.Configuration);

			var actual = source.Packs;
			Assert.IsNotNull(actual);
			Assert.That(actual, Is.EquivalentTo(expected).Using(new PackInfoEqualityComparer()));
		}
	}
}
